package cfgs.gestudent.dao.curs;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.ResourceBundle;

import javax.swing.JOptionPane;

import cfgs.gestudent.dao.connexio.ConnexioDB;
import cfgs.gestudent.preferencies.ConfiguracionsIdiomes;

public class MostrarDies {
	
	private ConfiguracionsIdiomes idioma = new ConfiguracionsIdiomes();
	private ResourceBundle rb = idioma.getIdioma();
	
	public ArrayList<String> llistesComboBox() {
		ArrayList<String> llista = new ArrayList<String>();

		Connection connexio = ConnexioDB.getConnexio();
		ResultSet rs = null;
		Statement st = null;

		String query = "SELECT Nom FROM DIES";
		
		try {
			st = connexio.createStatement();
			rs = st.executeQuery(query);
			while (rs.next()) {
				llista.add(rs.getString("Nom"));
			}
		} catch (SQLException e) {
			JOptionPane.showMessageDialog(null,
					rb.getString("missatgeErrorStatement") + "\n " + e,
					rb.getString("titolErrorStatement"),
					JOptionPane.ERROR_MESSAGE);
		} finally {
			try {
				connexio.close();
				st.close();
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
				JOptionPane
						.showMessageDialog(null,
								rb.getString("missatgeErrorTancarConnexio")
										+ "\n " + e,
								rb.getString("titolErrorTancarConnexio"),
								JOptionPane.ERROR_MESSAGE);
			}
		}

		return llista;
	}
	
	public int getIdDia(String dia) {
		
		int idDia = 0;

		Connection connexio = ConnexioDB.getConnexio();
		ResultSet rs = null;
		Statement st = null;

		String query = "SELECT Id FROM DIES WHERE Nom='"+dia+"'";
		
		try {
			st = connexio.createStatement();
			rs = st.executeQuery(query);
			while (rs.next()) {
				idDia = rs.getInt("Id");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,
					rb.getString("missatgeErrorStatement") + "\n " + e,
					rb.getString("titolErrorStatement"),
					JOptionPane.ERROR_MESSAGE);
		} finally {
			try {
				connexio.close();
				st.close();
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
				JOptionPane
						.showMessageDialog(null,
								rb.getString("missatgeErrorTancarConnexio")
										+ "\n " + e,
								rb.getString("titolErrorTancarConnexio"),
								JOptionPane.ERROR_MESSAGE);
			}
		}
		
		return idDia;

	}
	
public String getNomDia(int dia) {

		String nomDia = null;

		Connection connexio = ConnexioDB.getConnexio();
		ResultSet rs = null;
		Statement st = null;

		String query = "SELECT Nom FROM DIES WHERE Id='"+dia+"'";
		
		try {
			st = connexio.createStatement();
			rs = st.executeQuery(query);
			while (rs.next()) {
				nomDia = rs.getString("Nom");
			}
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null,
					rb.getString("missatgeErrorStatement") + "\n " + e,
					rb.getString("titolErrorStatement"),
					JOptionPane.ERROR_MESSAGE);
		} finally {
			try {
				connexio.close();
				st.close();
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
				JOptionPane
						.showMessageDialog(null,
								rb.getString("missatgeErrorTancarConnexio")
										+ "\n " + e,
								rb.getString("titolErrorTancarConnexio"),
								JOptionPane.ERROR_MESSAGE);
			}
		}
		
		return nomDia;

	}

}
